package com.fix.util;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author Octavio Kidd
 * @since 06-Jul-2012
 */
public final class IOHelper {
	private static final Log LOGGER = LogFactory.getLog(IOHelper.class);
	
	/**
	 * Closes one or more {@link java.io.OutputStream OutputStream}s, and logs
	 * any exceptions thrown in the process.
	 * 
	 * @param outStreams The {@link java.io.OutputStream OutputStream}s to close.
	 */
	public static void closeOutputStreams(OutputStream ... outStreams){
		for (OutputStream os : outStreams) {
			try {
				os.close();
			} catch (Exception ex) {
				LOGGER.debug("Error intentando cerrar un Output Stream", ex);
			}
		}
	}
	
	/**
	 * Closes one or more {@link java.io.InputStream InputStream}s, and logs
	 * any exceptions thrown in the process.
	 * 
	 * @param inStreams The {@link java.io.InputStream InputStream}s to close.
	 */
	public static void closeInputStreams(InputStream ... inStreams) {
		for (InputStream is : inStreams) {
			try {
				is.close();
			} catch (Exception ex) {
				LOGGER.debug("Error intentando cerrar un Input Stream", ex);
			}
		}
	}
	
	/**
	 * Closes one or more stream {@link java.io.Writer Writer}s, and logs
	 * any exceptions thrown in the process.
	 * 
	 * @param writers The stream {@link java.io.Writer Writer}s to close.
	 */
	public static void closeStreamWriters(Writer ... writers){
		for (Writer writer : writers) {
			try {
				writer.close();
			} catch (Exception ex) {
				LOGGER.debug("Error intentando cerrar un Output Stream", ex);
			}
		}
	}
	
	/**
	 * Closes one or more stream {@link java.io.Reader Reader}s, and logs
	 * any exceptions thrown in the process.
	 * 
	 * @param readers The stream {@link java.io.Reader Reader}s to close.
	 */
	public static void closeStreamReaders(Reader ... readers) {
		for (Reader reader : readers) {
			try {
				reader.close();
			} catch (Exception ex) {
				LOGGER.debug("Error intentando cerrar un Input Stream", ex);
			}
		}
	}
}
